// orderApi.ts

import { ApiResponse } from "apisauce"

import {
  GetProductsResp,
  CreateOrderReq,
  CreateOrderResp,
  CreateWxPaymentReq,
  CreateWxPaymentResp,
  CreateAliPaymentReq,
  CreateAliPaymentResp,
  VerifyIAPAppStoreTransactionReq,
  VerifyIAPGooglePlayPurchaseReq,
} from "@/models/Order"
import { apiSauce } from "@/services/api"

const PREFIX = "/gpt404/order"

// 获取产品列表
export const getProducts = async (params: {
  type: "member" | "coin"
}): Promise<ApiResponse<GetProductsResp>> => {
  return apiSauce.get(`${PREFIX}/products`, params)
}

// 创建订单
export const createOrder = async (
  payload: CreateOrderReq,
): Promise<ApiResponse<CreateOrderResp>> => {
  return apiSauce.post(`${PREFIX}/order`, payload)
}

// 获取订单
export const checkOrder = async (payload: any): Promise<ApiResponse<CreateOrderResp>> => {
  return apiSauce.get(`${PREFIX}/order?orderNum=${payload.orderNum}`)
}

// 获取核销码列表
export const getCDKS = (data: any) => {
  return apiSauce.get(`${PREFIX}/cdks`, { ...data })
}

// 核销核销码
export const consumeCDK = (data: any) => {
  return apiSauce.post(`${PREFIX}/consume_cdk`, data)
}

// 创建微信预付单
export const createWxPayment = async (
  payload: CreateWxPaymentReq,
): Promise<ApiResponse<CreateWxPaymentResp>> => {
  return apiSauce.post(`${PREFIX}/wx/payment`, payload)
}

// 支付宝支付
export const createAliPayment = async (
  payload: CreateAliPaymentReq,
): Promise<ApiResponse<CreateAliPaymentResp>> => {
  return apiSauce.post(`${PREFIX}/ali/payment`, payload)
}

// IAP (App Store) 交易验证
export const verifyIAPAppStoreTransaction = async (
  payload: VerifyIAPAppStoreTransactionReq,
): Promise<ApiResponse<any>> => {
  return apiSauce.post(`${PREFIX}/iap/appstore/transaction`, payload)
}

// IAP (App Store) 交易验证
export const verifyIAPGooglePlayPurchase = async (
  payload: VerifyIAPGooglePlayPurchaseReq,
): Promise<ApiResponse<any>> => {
  return apiSauce.post(`${PREFIX}/iap/google_play/purchase`, payload)
}
